<?php
require_once __DIR__ . '/../core/config.php';

// 用户会员 余额充值函数
function recharge($account, $key) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token, vip_time, user_balance FROM star_user WHERE account = :account");
        $stmt->bindParam(':account', $account, PDO::PARAM_STR);
        $stmt->execute();
        $user = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$user) {
            return json_response(500, '账号未注册');
        }
        if ($user['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁且权限不足!');
        }
        $current_time = time();
        $token_time = strtotime($user['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        $stmt = $pdo->prepare("SELECT key_time, key_balance FROM star_key WHERE some_key = ?");
        $stmt->execute([$key]);
        $key_info = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$key_info) {
            return json_response(500, '充值密钥不存在');
        }
        if ($key_info['key_time'] !== null) {
            $vip_time = new DateTime($user['vip_time']);
            $vip_time->modify("+{$key_info['key_time']} day");
            $stmt = $pdo->prepare("UPDATE star_user SET vip_time = ? WHERE account = ?");
            $stmt->execute([$vip_time->format('Y-m-d H:i:s'), $account]);
        } else {
            $new_balance = $user['user_balance'] + $key_info['key_balance'];
            $stmt = $pdo->prepare("UPDATE star_user SET user_balance = ? WHERE account = ?");
            $stmt->execute([$new_balance, $account]);
        }
        $stmt = $pdo->prepare("DELETE FROM star_key WHERE some_key = ?");
        $stmt->execute([$key]);

        return json_response(200, '充值成功');
    } catch (PDOException $e) {
        return json_response(500, '充值失败: ' . $e->getMessage());
    }
}

// 生成充值密钥函数
function key_create($account, $type, $quantity, $count) {
    $pdo = connect_db();
    try {
        $stmt = $pdo->prepare("SELECT user_permissions, user_token FROM star_user WHERE account = :account");
        $stmt->bindParam(':account', $account, PDO::PARAM_STR);
        $stmt->execute();
        $user = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$user) {
            return json_response(500, '账号未注册');
        }
        if ($user['user_permissions'] == 1) {
            return json_response(500, '账号权限不足');
        }
        if ($user['user_permissions'] == 2) {
            return json_response(500, '账号已被封禁且权限不足!');
        }
        $current_time = time();
        $token_time = strtotime($user['user_token']);
        if ($token_time < $current_time) {
            return json_response(500, '账号未登录');
        }
        if ($type !== 'time' && $type !== 'balance') {
            return json_response(500, '无效的类型');
        }
        for ($i = 0; $i < $count; $i++) {
            $some_key = generate_random_key();
            if ($type === 'time') {
                $stmt = $pdo->prepare("INSERT INTO star_key (some_key, key_time, key_balance) VALUES (?, ?, NULL)");
                $stmt->execute([$some_key, $quantity]);
            } else {
                $stmt = $pdo->prepare("INSERT INTO star_key (some_key, key_time, key_balance) VALUES (?, NULL, ?)");
                $stmt->execute([$some_key, $quantity]);
            }
        }
        return json_response(200, '充值密钥创建成功');
    } catch (PDOException $e) {
        return json_response(500, '创建充值密钥失败: ' . $e->getMessage());
    }
}

// 辅助函数：生成随机密钥
function generate_random_key() {
    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $part1 = substr(str_shuffle($characters), 0, 4);
    $part2 = substr(str_shuffle($characters), 0, 4);
    $part3 = substr(str_shuffle($characters), 0, 4);
    return $part1 . '-' . $part2 . '-' . $part3;
}